package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SafeQryEparchyByProvinceAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@AttrHandler(SafeQryEparchyByProvinceAttrHandler.class)
public class SafeQryEparchyByProvinceLinkAttrHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if(KiteStringUtils.equals(attr.getFieldName(),"210008107")){
            //接入地市
            KiteAttr eparchyAttr = inst.getAttr("200003627");
            String provinceCode = attr.getValue();
            if(Objects.nonNull(eparchyAttr)&& KiteStringUtils.isNotEmpty(provinceCode)){
                String eparchyKeyValue = attr.getValue();
                //选择省外取100
                if(KiteStringUtils.equals(provinceCode,"-100")){
                    eparchyKeyValue = "100";
                }
                List<AttrValue> attrValues = AttrValueCache.get(KiteStringUtils.concatWithSymbol(attr.getAttrId(), eparchyKeyValue, ""));
                if(KiteListUtils.isNotEmpty(attrValues)){
                    eparchyAttr.setValue(attrValues.get(0).getAttrValue());
                    eparchyAttr.setValueDesc(attrValues.get(0).getAttrValueDesc());
                    eparchyAttr.setAttrValues(KiteBeanUtils.copyPropsForList(attrValues, KiteAttrValue.class));
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
